package util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.List;

import static util.TxtHelper.readTxt;

public class ComplexJsonTest {
    public static void main(String[] args) throws IOException{
        //读取文件内容。该JSON字串中有两个对象：普通对象pagination和数组conditionList
        String json = readTxt("files/" + "complexJson.txt");
        System.out.println(json);
        //从请求JSON字串中获得conditionList对象(JSONArray对象)
        JSONArray conditionList_jsonArray =
                JSON.parseObject(json).getJSONArray("conditionList");
        String conditoinList_json = conditionList_jsonArray.toString();
        System.out.println(conditoinList_json);
        //JSONArray对象转换为Java的List对象
        List<Condition> conditionList = conditionList_jsonArray.toJavaList(Condition.class);
        for(Condition c: conditionList){
            System.out.println(c.getKey()+ c.getOperator()+c.getValue());
        }

        //从请求数据中读取 pagination 对象
        JSONObject pagination_jsonObject = JSON.parseObject(json).getJSONObject("pagination");
        //将 JSON 对象转换为 Java 对象
        Pagination pagination = pagination_jsonObject.toJavaObject(Pagination.class);
        System.out.println(pagination.getPageNo());
        System.out.println(pagination.getPageSize());


    }

}
